package com.smm.web.vmc.util;


import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.smm.web.vmc.domain.User;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 发送手机短信工具类
 */
@Slf4j
public class SMSUtil {

    private static final String product = "Dysmsapi";
    private static final String domain = "dysmsapi.aliyuncs.com";
    private static final String accessKeyId = "LTAIzwfHvxgYazBv";
    private static final String accessKeySecret = "OCtqOTmtmzBdnXWhQzdh6GQ9ds8Ss6";

    public static void send(List<User> userList, String no, Date date) throws Exception {

        if(userList==null || userList.size()==0) throw new Exception("仓单"+no+"没有关联用户");

        StringBuilder phones = new StringBuilder();

        //获取手机号码
        for(User user:userList){

            if(StringUtils.isNotBlank(user.getPhone())){

                phones.append(user.getPhone()).append(",");
            }
        }


        if(StringUtils.isNotBlank(phones)){

            String tempPhones = phones.substring(0,phones.length()-1);

            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            SMSUtil.sendSMS(tempPhones,no,format.format(date));
        }
    }


    private static void sendSMS(String phones, String receiptNo,String time) throws Exception{

        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId,accessKeySecret);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);
        SendSmsRequest request = new SendSmsRequest();
        request.setMethod(MethodType.POST);
        request.setSignName("金属云仓");
//        request.setTemplateCode("SMS_109335204");
        request.setTemplateCode("SMS_109540274");

        //可修改参数
//        request.setPhoneNumbers("13810329795,18939731511");
//        request.setTemplateParam("{\"name\":\"YSCU0001\", \"time\":\"11:20:50\"}");

        request.setPhoneNumbers(phones);
        request.setTemplateParam("{name: '"+receiptNo+"', time: '"+time+"'}");

        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);

        log.info("短信收件人："+ phones);

        log.info(sendSmsResponse.getCode());

        if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {

            log.info("短信发送成功. 收件收件号码："+phones + " 仓单编号："+receiptNo);
        }

    }


    public static void main(String[] args) throws Exception {

        String phones = "13758782020,15803522202,15918676640,15917593315,13671700801,18858182469,18768953551,13312345678,13806566352,18912345612,17602109795,13312345678,18939731511,18717804596,13312345678,13312345678,13312345678,13312345678";

        SMSUtil.sendSMS(phones,"","2017-11-09 17:26:33");
    }
}
